<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>输出文件</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.readfile" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">readfile</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">readfile</span> &mdash; <span class="dc-title">输出文件</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.readfile-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>readfile</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$use_include_path</code><span class="initializer"> = false</span></span>
   [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span>
  ]] ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   读取文件并写入到输出缓冲。
  </p>
</div>

 
   <div class="refsect1 parameters" id="refsect1-function.readfile-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">filename</code></dt>

     <dd>

      <p class="para">
       要读取的文件名。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">use_include_path</code></dt>

     <dd>

      <p class="para">
       想要在
   <a href="ini.core.html#ini.include-path" class="link">include_path</a>
   中搜索文件，可使用这个可选的第二个参数，设为 <strong><code>TRUE</code></strong>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">context</code></dt>

     <dd>

      <p class="para">
       Stream 上下文（context） <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.readfile-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回从文件中读入的字节数。如果出错返回
   <strong><code>FALSE</code></strong> 并且除非是以
   @<span class="function"><strong>readfile()</strong></span> 形式调用，否则会显示错误信息。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.readfile-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2835">
    <p><strong>Example #1 使用 <span class="function"><strong>readfile()</strong></span> 强制下载</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$file&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'monkey.gif'</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">file_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Description:&nbsp;File&nbsp;Transfer'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type:&nbsp;application/octet-stream'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Disposition:&nbsp;attachment;&nbsp;filename="'</span><span style="color: #007700">.</span><span style="color: #0000BB">basename</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">).</span><span style="color: #DD0000">'"'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Expires:&nbsp;0'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Cache-Control:&nbsp;must-revalidate'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Pragma:&nbsp;public'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Length:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">filesize</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">readfile</span><span style="color: #007700">(</span><span style="color: #0000BB">$file</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="mediaobject">
     
     <div class="imageobject">
      <img src="images/e88cefb5c3fca5060e2490b9763c4433-readfile.png" alt="打开 / 保存对话框" width="319" height="245" />
     </div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.readfile-notes">
  <h3 class="title">注释</h3>

 <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    <span class="function"><strong>readfile()</strong></span> 自身不会导致任何内存问题。
    如果出现内存不足的错误，使用 <span class="function"><a href="ob_get_level.html" class="function">ob_get_level()</a></span> 确保输出缓存已经关闭。
   </p>
  </p></blockquote>
  
  <div class="tip"><strong class="tip">Tip</strong><p class="simpara">如已启用<a href="filesystem.configuration.html#ini.allow_url_fopen" class="link">fopen 包装器</a>，在此函数中， URL 可作为文件名。关于如何指定文件名详见 <span class="function"><a href="fopen.html" class="function">fopen()</a></span>。各种
wapper 的不同功能请参见 <a href="wrappers.html" class="xref">支持的协议和封装协议</a>，注意其用法及其可提供的预定义变量。</p></div>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">在 PHP 5.0.0
中增加了对上下文（Context）的支持。有关<em>上下文（Context）</em>的说明参见
<a href="book.stream.html" class="xref">Streams</a>。</span></p></blockquote>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.readfile-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="fpassthru.html" class="function" rel="rdfs-seeAlso">fpassthru()</a> - 输出文件指针处的所有剩余数据</span></li>
    <li class="member"><span class="function"><a href="file.html" class="function" rel="rdfs-seeAlso">file()</a> - 把整个文件读入一个数组中</span></li>
    <li class="member"><span class="function"><a href="fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - 打开文件或者 URL</span></li>
    <li class="member"><span class="function"><a href="include.html" class="function" rel="rdfs-seeAlso">include</a> - include</span></li>
    <li class="member"><span class="function"><a href="require.html" class="function" rel="rdfs-seeAlso">require</a> - require</span></li>
    <li class="member"><span class="function"><a href="virtual.html" class="function" rel="rdfs-seeAlso">virtual()</a> - 执行 Apache 子请求</span></li>
    <li class="member"><span class="function"><a href="file_get_contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - 将整个文件读入一个字符串</span></li>
    <li class="member"><a href="wrappers.html" class="xref">支持的协议和封装协议</a></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>